$(function(){
	var $tabList=$("#tab_list"),$pagebar=$("#pagebar ul"),$pageInfo=$("#page_info"),
		pageNo=1,pageSize=10,count=0;//初始值
	/**---------------封装分页方法,count:总条目数,pageNo:当前页---------------*/
	function pageit(count){
		$pagebar.empty();
		//首页和上一页
		if(pageNo==1){
			$pagebar.append("<li id='start_page' class='disabled'><a href='javascript:void(0);'>首页</a></li>");
			$pagebar.append("<li id='prev_page'  class='disabled'><a href='javascript:void(0);'>上一页</a></li>");
		}else{
			$pagebar.append("<li id='start_page' pn=1><a href='javascript:void(0);'>首页</a></li>");
			$pagebar.append("<li id='prev_page' pn="+(pageNo-1)+"><a href='javascript:void(0);'>上一页</a></li>");
		}
		var tmp=parseInt(count/10);
		var page_size=count%10==0?(tmp):(tmp+1);//总页数
		//中间数字页
		if(page_size<=5){
			//如果小于5页,则全部显示
			for(var i = 0; i <page_size; i++) {
				if(i==pageNo-1){
					$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
				}else{
					$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
				}
			}
		}else{
			//如果总页数大于5页,显示当前页和前后各两页以及两个...符号
			if(pageNo<3){
				for(var i = 0; i <5; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
				$pagebar.append("<li class='disabled'><a href='javascript:void(0);'>...</a></li>");
			}else if(pageNo>page_size-2){
				$pagebar.append("<li class='disabled'><a href='javascript:void(0);'>...</a></li>");
				for(var i = page_size-5; i <page_size; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(i+1)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
			}else{
				for (var i = pageNo-3; i <pageNo+2; i++) {
					if(i==pageNo-1){
						$pagebar.append("<li pn="+(pageNo)+" class='active'><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}else{
						$pagebar.append("<li pn="+(i+1)+"><a href='javascript:void(0);'>"+(i+1)+"</a></li>");
					}
				}
			}
		}
		//下一页和末页
		if(page_size==0||pageNo==page_size){
			$pagebar.append("<li id='next_page' class='disabled'><a href='javascript:void(0);'>下一页</a></li>");
			$pagebar.append("<li id='end_page' class='disabled'><a href='javascript:void(0);'>末页</a></li>");
		}else{
			$pagebar.append("<li id='next_page' pn="+(pageNo+1)+"><a href='javascript:void(0);'>下一页</a></li>");
			$pagebar.append("<li id='end_page' pn="+page_size+"><a href='javascript:void(0);'>末页</a></li>");
		}
		
		//绑定事件
		$pagebar.find("li").not(".disabled").on("click",function(){
			pageNo=parseInt($(this).attr("pn"));//更新当前页码
			reloadList();
		});
		//分页信息条
		$pageInfo.find("span").each(function(i,k){
			if(i==0){
				if(page_size==0){
					$(k).html(0);
				}else{
					$(k).html((pageNo-1)*10+1);
				}
			}else if(i==1){
				$(k).html(count>pageNo*10?pageNo*10:count);
			}else{
				$(k).html(count);
			}
		});
	}
	/**------------------加载列表---------------------*/
	var params={"pageNo":1,"pageSize":pageSize,"status":0,"name":null,"phone":null};
	function reloadList(){
		params.pageNo=pageNo;
		params.pageSize=pageSize;
		$.get(serverURL+"pay/cashout/list",params,function(data){
			try {
				if(data.code==0){
					data=data.data;
					$tabList.find("tbody").empty();
					count=data.pagination.totalCount;
					if(count>0){
						var html="";
						$.each(data.list,function(i,k){
							html+="<tr>" +
							"<td id='pkc'><input type='checkbox' id='"+k.id+"' class='checkbox' col='"+i+"'></td>" +
							"<td>"+((pageNo-1)*10+i+1)+"</td>" +
							"<td>"+k.realName+"</td>"+ 
							"<td>"+(k.gender==0?"女":"男")+"</td>"+ 
							"<td>"+k.phone+"</td>"+ 
							"<td>"+k.coin+"</td>"+ 
							"<td>"+(k.openBank==null?"<span style='color:red;'>暂无</span>":k.openBank)+"</td>"+ 
							"<td>"+(k.cardNo==null?"<span style='color:red;'>暂无</span>":k.cardNo)+"</td>"+ 
							"<td>"+k.coinAmount/10+"</td>"+ 
							"<td>"+(k.status==0?"<span style='color:red;'>未处理</span>":"<span style='color:green;'>已处理</span>")+"</td>"+ 
							"<td>"+k.operateTime+"</td>"+ 
							"</tr>";
						});
						$tabList.find("tbody").append(html);
						$tabList.find("tr").dblclick(function(){
							showScanModal($(this).find("#pkc input").attr("id"));
						});
					}else{
						$tabList.find("tbody").append("<tr><td colspan="+$("thead th").length+">无数据</td></tr>");
					}
					pageit(count);//重绘工具栏
				}else if(data.code!=-1){
					alert("数据加载出错，请稍后重试");
				}
			} catch (e) {
				alert("数据加载出错，请稍后重试"+e);
			}
		});
	}
	reloadList();
	//全选与取消全选
	$("#pkall").change(function(){
		if($(this).prop("checked")){
			$tabList.find("#pkc :checkbox").prop("checked",true);
		}else{
			$tabList.find("#pkc :checkbox").prop("checked",false);
		}
	});
	//获取选中的行
	function getSelectedCols(){
		return $tabList.find("#pkc :checkbox").filter(":checked");
	}
	function getSelectIds(){
		var ids="",$selects=getSelectedCols();
		$selects.each(function(i,k){
			ids+=k.id+",";
		});
		ids=ids.substring(0, ids.length-1);
		return ids;
	}
	/**---------------查询-----------------*/
	$("#btn_search").click(function(){
		params.name=$("#search_name").val();
		params.phone=$("#search_phone").val();
		pageNo=1;
		reloadList();
		return false;
	});
	/**---------------重置-----------------*/
	$("#btn_reset").click(function(){
		$("#form_search").get(0).reset();
		params.name=null;
		params.phone=null;
		pageNo=1;
		reloadList();
		return false;
	});
	/**---------------状态过滤-----------------*/
	$("#btn_status button").click(function(){
		$(this).addClass("active").siblings().removeClass("active");
		params.pageNo=1;
		params.status=$(this).attr("id");
		if($(this).attr("id")==0){
			$("#btn_solve button").removeAttr("disabled");
		}else{
			$("#btn_solve button").prop("disabled","disabled");
		}
		reloadList();
	});
	
	$("#btn_excel").click(function() {
		window.location.href=serverURL+"pay/cashout/excel?status="+ params.status;
	});
	
	//解决待处理提现
	$("#btn_solve").click(function() {
		var $curCol =getSelectedCols();
		if ($curCol.length == 0) {
			alert("请请至少选择一条记录解决");
			return;
		}
		var id = "";
		$.each($curCol,function(i, k) {
            	id +=k.id+",";
		});
		id = id.substring(0, id.lastIndexOf(","));
		$.post(serverURL + "pay/cashout/solve", {"ids" : id}, function(data) {
			if (data.code == 0) {
				alert("提现处理成功 !");
				reloadList();
			}else if(data.code!=-1){
				alert("提现处理失败");
			}
		});

	});
	/**---------------查看详情-----------------*/
	$modalScan=$("#modal_scan"),$scanName=$modalScan.find("[name=realName]"),$scanGender=$modalScan.find("[name=gender]"),
			  $scanPhone=$modalScan.find("[name=phone]"),$scanCoin=$modalScan.find("[name=coin]"),$scanCoin2=$modalScan.find("[name=coin2]"),
			  $scanOpenBank=$modalScan.find("[name=openBank]"),$scanCardNo=$modalScan.find("[name=cardNo]"),
			  $scanAmount=$modalScan.find("[name=amount]"),$scanApplyTime=$modalScan.find("[name=applyTime]");
	function showScanModal(id){
		$.get(serverURL + "pay/cashout/get", {"id" : id}, function(data) {
			if(data.code==0){
				data=data.data;
				$modalScan.modal("show");
				$scanName.val(data.realName);
				$scanGender.val(data.gender==0?"女":"男");
				$scanPhone.val(data.phone);
				$scanCoin.val(data.coin);
				$scanCoin2.val(data.coinAmount);
				$scanOpenBank.val(data.openBank);
				$scanCardNo.val(data.cardNo);
				$scanAmount.val((data.coinAmoint)/10);
				$scanApplyTime.val(data.operateTime);
			}else if(data.code!=-1){
				alert("数据加载失败，请重试");
			}
		});
	}
	$("#btn_scan").click(function(){
		var $selects=getSelectedCols();
		if($selects.length!=1){
			alert("请选择一条数据查看,双击记录行也可查看");
			return;
		}
		showScanModal($selects.get(0).id);
	});
});